// pages/profile/profile.js
const app = getApp()
var util =  require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['男', '女'],
    downImg: '/resources/down.png',
    upImg: '/resources/up.png',
    sex:'',
    date:'',
    stature: '',
    nickName: '',
    IsInput:false,
    btnTxt:{
      btnTxt:'保存'
    },
    isHeight:false,
    styles: {
      line: '#d0d0d0',
      bginner: '#fbfbfb',
      bgoutside: '#ffffff',
      lineSelect: '#2897ff',
      font: '#000000'
    },
  },

  //picker
  bindSexChange: function (e) {
    this.setData({
      index: e.detail.value,
      sex: this.data.array[e.detail.value],
      sexImgsrc: this.data.downImg,
    })
  },

  bindSexCancel:function(){
    this.setData({
      sexImgsrc: this.data.downImg
    })
  },

  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value,
      dateImgsrc: this.data.downImg
    })
  },

  bindDateCancel:function(){
    this.setData({
      dateImgsrc: this.data.downImg
    })
  },

  //选中的身高
  bindvalue: function (e) {
    this.setData({
      value: e.detail.value,
    })
  },

  // // 获取昵称
  // nicknameInput :function (e) { 
  //   this.setData({ 
  //     nickname:e.details.value
  //   }) 
  // }, 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIndexList();
    this.setData({
      userInfo: app.globalData.userInfo,
      heightImgsrc: this.data.downImg,
      sexImgsrc: this.data.downImg,
      dateImgsrc: this.data.downImg,
    })
  },

  getIndexList:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/user',
      data: {
        id: app.globalData.userId,
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res);
        if(res.data.code == '200'){
          var date = res.data.data.birthday;
          date = date.substring(0, 10);
          var sex = res.data.data.sex;
          var index = ''
          if(sex == '男')
          {
            index = 0
          }
          else
          {
            index = 1
          }
          that.setData({
            stature: res.data.data.height,
            nickName: that.data.userInfo.nickName,
            date: date,
            index: index,
            sex: res.data.data.sex
          })
          if(that.data.stature!= '')
          {
            that.setData({
              weightMax: that.data.stature * 2
            })
            // console.log(this.data.weightMax)
          }
        }
      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },

  //选择身高
  selectHeight: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.anim(currentStatu);
  },

  //动画部分
  anim: function (currentStatu) {
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });
    
    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;
 
    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是240px)，停
    animation.translateY(280).step();
 
    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })
    
    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })
      
      //完成
      if (currentStatu == "complete") {
        this.setData(
          {
            isHeight: false,
            stature: this.data.value,
            heightImgsrc: this.data.downImg,
            weightMax: this.data.value * 2
          }
        );
      }

      //取消
      else if (currentStatu == "cancel") {
        this.setData(
          {
            isHeight: false,
            heightImgsrc: this.data.downImg
          }
        );
      }
    }.bind(this), 200)
  
    // 显示抽屉
    if (currentStatu == "open") {
      this.setData(
        {
          isHeight: true,
          heightImgsrc: this.data.upImg
        }
      );
    }
  },

  //阻止穿透
  preventTouchMove: function () {
    // console.log('stop user scroll it!');
    return;
  },

  //选择箭头
  imgChange:function(e)
  {
    var that = this
    if(e.currentTarget.dataset.type == "性别")
    {
      if(that.data.sexImgsrc == that.data.downImg)
      {
        that.setData({
          sexImgsrc: that.data.upImg
        })
      }
    }
    else if(e.currentTarget.dataset.type == "日期"){
      if(that.data.dateImgsrc == that.data.downImg)
      {
        that.setData({
          dateImgsrc: that.data.upImg
        })
      }
    }
  },

  //保存
  lickBtn: function(){
    var that = this
    // if(that.data.nickname == ''){
    //   util.showModal('提示','请填写昵称')
    //   return;
    // }
    if(that.data.sex == ''){
      util.showModal('提示','请选择性别')
      return;
    }
    if(that.data.stature == '')
    {
      util.showModal('提示','请选择体重')
      return;
    }
    if(that.data.date == '')
    {
      util.showModal('提示','请选择出生日期')
      return;
    }
    wx.request({
      url: app.globalData.apiUrl + '/user', //本地服务器地址
      data: {
        userId: app.globalData.userId,
        sex: that.data.sex,
        birthday: that.data.date,
        height: that.data.stature,
      },
      method: 'put',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        if(res.data.code == '200')
        {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
          })
          setTimeout(function () {
            wx.hideToast()
          }, 1000)
        }else{
          util.showModal('提示','保存失败，请重新保存')
          return
        }
      },
      fail: function (res) {
        util.showModal('提示','保存失败，请重新保存')
        return
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'PAPN小程序',
      path: 'pages/index/index?puid=' + app.globalData.userId,
      success: function(res) {
          res.shareTickets 
      },
      fail: function(res) {
          console.log(res)
      }
    }
  }
})